package serialize.jackson;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.util.Arrays;

public class JacksonDemo {

    /**
     * 下面会反序列化报错
     *  解决办法：
     *  1.给Object设置mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); 表示字段不识别时不报错
     *  2.加这个注解@JsonIgnoreProperties(ignoreUnknown = true)
     */
    private ObjectMapper objectMapper = new ObjectMapper();

    @Test
    void objectToJson() throws IOException {
        //objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        //定义personA
        PersonA personA = new PersonA();
        personA.setName("LaoWang");
        personA.setAge(28);
        personA.setSkillList(Arrays.asList("java", "c++"));
        String jsonA = objectMapper.writeValueAsString(personA);


        //定义B
        PersonB personB = objectMapper.readValue(jsonA, PersonB.class);
        System.out.println(objectMapper.writeValueAsString(personB));
    }

}
